package com.zymail.service;

import java.util.ArrayList;

import com.zymail.bean.ZyClass;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zymail.bean.ZyGood;
import com.zymail.bean.ZyGoodDe;
import com.zymail.bean.ZyGoodImg;
import com.zymail.service.impl.GoodSonService;
import com.zymail.vo.GoodInfo;

@Service
@Slf4j
public class SaveGoodInfo implements GoodImgService, GoodService, GoodSonService {

    @Autowired
    ZyGoodImgService zyGoodImgService;

    @Autowired
    ZyGoodService    zyGoodService;

    @Autowired
    ZyGoodDeService  zyGoodDeService;

    @Autowired
    ZyClassService zyClassService;


    @Override
    public ZyGoodImg saveGoodImg(ZyGoodImg zyGoodImg) {
        zyGoodImgService.saveOrUpdate(zyGoodImg);
        return zyGoodImg;
    }

    @Override
    public ZyGood saveGood(ZyGood zyGood) {
        boolean b = zyGoodService.saveOrUpdate(zyGood);
        QueryWrapper<ZyGood> queryWrapper = new QueryWrapper<>();
        queryWrapper.setEntity(zyGood);
        return zyGood;
    }

    @Override
    public ZyGoodDe saveGoodSon(ZyGoodDe zyGoodDe) {
        zyGoodDeService.saveOrUpdate(zyGoodDe);
        return zyGoodDe;
    }

    public void saveGoodAllInfo(GoodInfo goodInfo) {
        ZyGood zyGood = new ZyGood();
        zyGood.setDe(goodInfo.getDetails());
        zyGood.setName(goodInfo.getName());

        String category = goodInfo.getCategory();
        QueryWrapper<ZyClass> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name",category);
        ZyClass one = zyClassService.getOne(queryWrapper);
        zyGood.setClassId(one.getId());

        log.info("get zyGood by goodinfo :" + zyGood.toString());

        ZyGood zyGood1 = saveGood(zyGood);

        log.info("save zyGood and get " + zyGood1.toString());

        String imageurl = goodInfo.getImage();
        String[] imagesurls = imageurl.split(";");

//        ArrayList<ZyGoodImg> zyGoodImgs = new ArrayList<>();

        for (String url : imagesurls) {
            ZyGoodImg zyGoodImg = new ZyGoodImg();
            zyGoodImg.setGoodId(zyGood1.getId());
            zyGoodImg.setUrl(url);
            zyGoodImg.setType(1);
            ZyGoodImg zyGoodImg1 = saveGoodImg(zyGoodImg);
            log.info("save goodImg + " + zyGoodImg1.toString());

//            zyGoodImgs.add(zyGoodImg);
            zyGoodImg.setId(null);
            zyGoodImg.setType(2);
            ZyGoodImg zyGoodImg2 = saveGoodImg(zyGoodImg);
            log.info("save goodImg + " + zyGoodImg2.toString());
//            zyGoodImgs.add(zyGoodImg);
        }

        String[] prices = goodInfo.getPrice().split(";");
        String[] stocks = goodInfo.getStock().split(";");
        String[] des = goodInfo.getSpeci().split(";");

        int len = prices.length;
        for (int i = 0; i < len; ++i) {
            ZyGoodDe zyGoodDe = new ZyGoodDe();
            zyGoodDe.setGoodId(zyGood1.getId());
            zyGoodDe.setName(zyGood1.getName()+des[i]);
            zyGoodDe.setCount(Integer.valueOf(stocks[i]));
            zyGoodDe.setPrice(Double.valueOf(prices[i]));
            zyGoodDe.setDe(des[i]);
            ZyGoodDe zyGoodDe1 = saveGoodSon(zyGoodDe);
            log.info("save zyGoodDe + " + zyGoodDe1.toString());
        }

    }

    public ZyGoodImg ParseAndGetGoodImg(GoodInfo goodInfo) {


//        zyGood.setClassId();
        String imageurl = goodInfo.getImage();
        String[] imagesurls = imageurl.split(";");
        ArrayList<ZyGoodImg> zyGoodImgs = new ArrayList<>();

        for (String url : imagesurls) {
            ZyGoodImg zyGoodImg = new ZyGoodImg();
//            zyGoodImg.se
        }
        return null;
    }

    public ZyGood getZyGood(GoodInfo goodInfo) {
        return null;
    }

    public ZyGoodDe getZyGoodDe(GoodInfo goodInfo) {
        return null;
    }
}
